﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsOrderManagement
{
    public partial class SelForm : Form
    {
        public SelForm()
        {
            InitializeComponent();
        }

        //确定查询：
        private async void button2_Click(object sender, EventArgs e)
        {
            List<Order> result = null;
            string sletype = comboBox1.Text;
            if (sletype == "查询所有订单")
            {
                result = initial.orderService.orders;
                if (result == null)
                {
                    SelError_1 error1 = new SelError_1();
                    error1.ShowDialog();
                }
                else
                {
                    LoadData1(result);
                    label3.Text = "订单查询成功！";
                    await Task.Delay(2000);//等两秒
                    label3.Text = "这里是信息提示";
                }
               
            }
            else if (sletype == "按照订单编号查询")
            {
                int inId = int.Parse(textBox1.Text);
                if (inId < 0 || inId > initial.OrderID)
                {
                    SelErrorForm sel = new SelErrorForm();
                    sel.ShowDialog();
                }
                else
                {
                    result = initial.orderService.QueryByOrderId(textBox1.Text);
                    if (result == null)
                    {
                        SelError_1 error1 = new SelError_1();
                        error1.ShowDialog();
                    }
                    else
                    {   LoadData1(result);
                        label3.Text = "订单查询成功！";
                        await Task.Delay(2000);//等两秒
                        label3.Text = "这里是信息提示";
                    }
                    
                }
                
            }
            else if (sletype == "按照商品名称查询")
            {
                string productName = textBox1.Text;
                if (productName == "苹果" || productName == "香蕉" || productName == "橘子" || productName == "草莓")
                {
                    result = initial.orderService.QueryByProductName(productName);
                    if (result == null)
                    {
                        SelError_1 error1 = new SelError_1();
                        error1.ShowDialog();
                    }
                    else
                    {
                        LoadData1(result);
                        label3.Text = "订单查询成功！";
                        await Task.Delay(2000);//等两秒
                        label3.Text = "这里是信息提示";
                    }
                }
                else 
                {
                    SelErrorForm sel1 = new SelErrorForm();
                    sel1.ShowDialog();
                }
            }
            else if (sletype == "按照客户名字查询")
            {
                string CustomerName = textBox1.Text;
                if (CustomerName == "甲" || CustomerName == "乙" || CustomerName == "丙")
                {
                    result = initial.orderService.QueryByCustomer(CustomerName);
                    if (result == null)
                    {
                        SelError_1 error1 = new SelError_1();
                        error1.ShowDialog();
                    }
                    else
                    {
                        LoadData1(result);
                        label3.Text = "订单查询成功！";
                        await Task.Delay(2000);//等两秒
                        label3.Text = "这里是信息提示";
                    }
                }
                else
                {
                    SelErrorForm sel1 = new SelErrorForm();
                    sel1.ShowDialog();
                }
            }
            else if (sletype == "按照订单金额范围查询")
            {
                decimal minAmount = decimal.Parse(textBox2.Text);
                decimal maxAmount = decimal.Parse(textBox3.Text);
                result = initial.orderService.QueryByAmountRange(minAmount, maxAmount);
                if (result == null)
                {
                    SelError_1 error1 = new SelError_1();
                    error1.ShowDialog();
                }
                else
                {
                    LoadData1(result);
                    label3.Text = "订单查询成功！";
                    await Task.Delay(2000);//等两秒
                    label3.Text = "这里是信息提示";
                }
            }
            else
            {
                SelErrorForm sel1 = new SelErrorForm();
                sel1.ShowDialog();
            }
        }


        //这个不用
        private void SelForm_Load(object sender, EventArgs e)
        {
        }

        private void LoadData1(List<Order>orders)
        {    // 将 Order 列表转换为扁平化的匿名对象列表
            var data = orders
                .Select(o => new
                {
                    OrderID = o.OrderId,
                    CustomerName = o.Customer.Name,
                    OrderDate = o.OrderDate,
                    ProductName = o.OrderDetails.Product.NAME,
                    Quantity = o.OrderDetails.Quantity,
                    Price = o.OrderDetails.Product.Price
                })
                .ToList();

            // 清空所有现有列（包括手动添加的）
            dataGridView1.Columns.Clear();

            // 允许自动生成列
            dataGridView1.AutoGenerateColumns = true;

            // 绑定到 DataGridView
            dataGridView1.DataSource = data;

            // 可选：调整列标题和格式
            dataGridView1.Columns["OrderID"].HeaderText = "订单号";
            dataGridView1.Columns["CustomerName"].HeaderText = "客户";
            dataGridView1.Columns["OrderDate"].HeaderText = "日期";
            dataGridView1.Columns["ProductName"].HeaderText = "商品";
            dataGridView1.Columns["Quantity"].HeaderText = "数量";
            dataGridView1.Columns["Price"].HeaderText = "单价";
            dataGridView1.Columns["OrderDate"].DefaultCellStyle.Format = "yyyy-MM-dd";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
